package com.kuang.servlet.user;

import com.kuang.pojo.User;
import com.kuang.service.user.UserServiceImpl;
import com.kuang.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet--start--");
//        获取用户和密码
        String userCode=req.getParameter("userCode");
        String userPassword=req.getParameter("userPassword");
//        调用业务层然后对比
        UserServiceImpl userService = new UserServiceImpl();
        User user = userService.login(userCode, userPassword);//这里已经吧登录的人查出来了
        System.out.println(userCode);
//    admin    输入的密码
        System.out.println(userPassword);

        System.out.println(user);
        System.out.println(user.getUserCode()+user.getUserName()+user.getUserPassword());


        if (user !=null &&user.getUserPassword().equals(userPassword)){//查到了有这个人
//            将查到的人放到Session中
            req.getSession().setAttribute(Constants.USER_SESSION,user);
//            跳转到内部主页
            resp.sendRedirect("jsp/frame.jsp");


        }else {//查无此人，无法登录 转发回登陆页面 提示错误信息
            System.out.println("错误");
            req.setAttribute("error","用户名或者密码错误");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
